using System;
using System.Collections.Generic;
using System.Data;

using Elev.Core;

namespace Elev.Core.Generator
{
    public class GridGenerator
    {
        /// <summary>
        /// Returns the grid of marks for a given period
        /// The DataTable returned's structure is :
        ///     * First row, each Exam instances
        ///     * Following rows, Grade instances. The order of the grade is Student ordered by lastname,
        ///         as in the list
        /// </summary>
        public Grid Generate(Period aPeriod)
        {
            Grid grid = new Grid(aPeriod);

            ExamsFactory eFactory = new ExamsFactory();
            List<Exam> exams = eFactory.GetExams(aPeriod.ID);

            if (exams.Count > 0)
            {
                // Append all the DataTable columns ...
                foreach (Exam exam in exams)
                    grid.AddColumn(exam);

                StudentsFactory sFactory = new StudentsFactory();
                List<Student> students = sFactory.GetStudents();

                foreach (Student student in students)
                {
                    GradesFactory gFactory = new GradesFactory();
                    List<Grade> grades = gFactory.GetGrades(student.ID, aPeriod.ID);

                    grid.AddRow(student, grades);
                }
            }

            return grid;
        }
    }
}